﻿using SrBBSPro.Services;
using System.IO;
using System.Web.Mvc;
using SrBBSPro.Entities;
using SrBBSPro.Models;
using SrBBSPro.BaseClasses;
using System;

namespace SrBBSPro.Controllers
{
    public class SkillController : BaseController
    {

        public ActionResult Index()       
            => View();
        
        public ActionResult DataList()
        {
            var depart = Request["Depart"];
            var kind = Request["kind"];
            var page = Convert.ToInt32(Request["page"]);
            switch (kind)
            {                
                case "OSSkill":
                    return JsonResultForLay<OSSkill,string>(new SkillService<OSSkill>().GetList(depart,out _, page));
                case "ServerSkill":
                    return JsonResultForLay<ServerSkill,string>(new SkillService<ServerSkill>().GetList(depart, out _, page));
                case "DBOSkill":
                    return JsonResultForLay<DBOSkill,string>(new SkillService<DBOSkill>().GetList(depart, out _, page));
                case "DBHSkill":
                    return JsonResultForLay<DBHSkill,string>(new SkillService<DBHSkill>().GetList(depart, out _, page));
                case "PhoneSkill":
                    return JsonResultForLay<PhoneSkill,string>(new SkillService<PhoneSkill>().GetList(depart, out _, page));
                case "HostLang":
                    return JsonResultForLay<HostLang,string>(new SkillService<HostLang>().GetList(depart, out _, page));
                case "OpenLang":
                    return JsonResultForLay<OpenLang,string>(new SkillService<OpenLang>().GetList(depart, out _, page));
                case "PageSkill":
                    return JsonResultForLay<PageSkill,string>(new SkillService<PageSkill>().GetList(depart, out _, page));
                case "ScriptLang":
                    return JsonResultForLay<ScriptLang,string>(new SkillService<ScriptLang>().GetList(depart, out _, page));
                case "ReportSkill":
                    return JsonResultForLay<ReportSkill,string>(new SkillService<ReportSkill>().GetList(depart, out _, page));
                case "OtherLang":
                    return JsonResultForLay<OtherLang,string>(new SkillService<OtherLang>().GetList(depart, out _, page));
                case "CloudLang":
                    return JsonResultForLay<CloudLang, string>(new SkillService<CloudLang>().GetList(depart, out _, page));
                case "Experience":
                    return JsonResultForLay<Experience, string>(new SkillService<Experience>().GetList(depart, out _, page));
                case "FrameWork":
                    return JsonResultForLay<FrameWork, string>(new SkillService<FrameWork>().GetList(depart, out _, page));
                default:
                    return JsonResultForLay<JapLang, string>(new SkillService<JapLang>().GetList(depart, out _, page));
            }
        }

        [HttpPost]
        public ActionResult SkillNames(string kind)
            => JsonResultForLay(kind);
        
        public FileStreamResult ExcelOut()
        {
            var path = new SkillService<JapLang>().GetExcelFile(HttpContext.Request.PhysicalApplicationPath);
            if (!string.IsNullOrEmpty(path))
            {
                FileStream fs = new FileStream(path, FileMode.Open);

                return File(fs, "text/plain", Path.GetFileName(path));
            }
            return null;
        }

        public ActionResult Detail()
            => View();
        

        [HttpPost]
        public ActionResult SkillEntity(string kind,string userId)
        {          
            if (string.IsNullOrEmpty(userId))
                return JsonResultForFly(1, "参数错误，请重新尝试");
            switch (kind)
            {              
                case "OSSkill":
                    return JsonResultForLay(new SkillService<OSSkill>().GetSkillEntity(userId));
                case "ServerSkill":
                    return JsonResultForLay(new SkillService<ServerSkill>().GetSkillEntity(userId));
                case "DBOSkill":
                    return JsonResultForLay(new SkillService<DBOSkill>().GetSkillEntity(userId));
                case "DBHSkill":
                    return JsonResultForLay(new SkillService<DBHSkill>().GetSkillEntity(userId));
                case "PhoneSkill":
                    return JsonResultForLay(new SkillService<PhoneSkill>().GetSkillEntity(userId));
                case "HostLang":
                    return JsonResultForLay(new SkillService<HostLang>().GetSkillEntity(userId));
                case "OpenLang":
                    return JsonResultForLay(new SkillService<OpenLang>().GetSkillEntity(userId));
                case "PageSkill":
                    return JsonResultForLay(new SkillService<PageSkill>().GetSkillEntity(userId));
                case "ScriptLang":
                    return JsonResultForLay(new SkillService<ScriptLang>().GetSkillEntity(userId));
                case "ReportSkill":
                    return JsonResultForLay(new SkillService<ReportSkill>().GetSkillEntity(userId));             
                case "OtherLang":
                    return JsonResultForLay(new SkillService<OtherLang>().GetSkillEntity(userId));
                case "CloudLang":
                    return JsonResultForLay(new SkillService<CloudLang>().GetSkillEntity(userId));
                case "Experience":
                    return JsonResultForLay(new SkillService<Experience>().GetSkillEntity(userId));
                case "FrameWork":
                    return JsonResultForLay(new SkillService<FrameWork>().GetSkillEntity(userId));
                default:
                    return JsonResultForLay(new SkillService<JapLang>().GetSkillEntity(userId));
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult JapLang(JapLangModel model)
            => EditEntity(model, "JapLang");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DBHSkill(DBHSkillModel model)
            => EditEntity(model, "DBHSkill");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DBOSkill(DBOSkillModel model)
            => EditEntity(model, "DBOSkill");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult OSSkill(OSSkillModel model)
            => EditEntity(model, "OSSkill");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ServerSkill(ServerSkillModel model)
            => EditEntity(model, "ServerSkill");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult PhoneSkill(PhoneSkillModel model)
            => EditEntity(model, "PhoneSkill");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult HostLang(HostLangModel model)
            => EditEntity(model, "HostLang");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult PageSkill(PageSkillModel model)
            => EditEntity(model, "PageSkill");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult OpenLang(OpenLangModel model)
            => EditEntity(model, "OpenLang");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ScriptLang(ScriptLangModel model)
            => EditEntity(model, "ScriptLang");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ReportSkill(ReportSkillModel model)
            => EditEntity(model, "ReportSkill");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult OtherLang(OtherLangModel model)
            => EditEntity(model, "OtherLang");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CloudLang(CloudLangModel model)
           => EditEntity(model, "CloudLang");

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Experience(ExperienceModel model)
           => EditEntity(model, "Experience");


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult FrameWork(FrameWorkModel model)
           => EditEntity(model, "FrameWork");

        private ActionResult EditEntity(object model, string kind)
        {
            if (model == null)
                return JsonResultForFly(1, "参数错误，请重新尝试");
            Response response;
            switch (kind)
            {              
                case "OSSkill":
                    response = new SkillService<OSSkill>().InsertOrUpdate( model);
                    break;
                case "ServerSkill":
                    response = new SkillService<ServerSkill>().InsertOrUpdate(model);
                    break;
                case "DBOSkill":
                    response = new SkillService<DBOSkill>().InsertOrUpdate(model);
                    break;
                case "DBHSkill":
                    response = new SkillService<DBHSkill>().InsertOrUpdate(model);
                    break;
                case "PhoneSkill":
                    response = new SkillService<PhoneSkill>().InsertOrUpdate(model);
                    break;
                case "HostLang":
                    response = new SkillService<HostLang>().InsertOrUpdate(model);
                    break;
                case "OpenLang":
                    response = new SkillService<OpenLang>().InsertOrUpdate(model);
                    break;
                case "PageSkill":
                    response = new SkillService<PageSkill>().InsertOrUpdate(model);
                    break;
                case "ScriptLang":
                    response = new SkillService<ScriptLang>().InsertOrUpdate(model);
                    break;
                case "ReportSkill":
                    response = new SkillService<ReportSkill>().InsertOrUpdate(model);
                    break;               
                case "OtherLang":
                    response = new SkillService<OtherLang>().InsertOrUpdate(model);
                    break;
                case "CloudLang":
                    response= new SkillService<CloudLang>().InsertOrUpdate(model);
                    break;
                case "Experience":
                    response = new SkillService<Experience>().InsertOrUpdate(model);
                    break;
                case "FrameWork":
                    response = new SkillService<FrameWork>().InsertOrUpdate(model);
                    break;
                default:
                    response = new SkillService<JapLang>().InsertOrUpdate(model);
                    break;
            }
            return JsonResultForFly(response.IsSuccess ? 0 : 1, response.Message, "/skill/detail?uid=" + CurrentUserId+"&kind="+ kind+"&sname="+CurrentAccount);
        }
    }

}